package cn.xiehang.SoundSight;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

public class RecyclerViewAdapter2  extends RecyclerView.Adapter<RecyclerViewAdapter2.RecyclerViewHolder> {
    private List<Product> mData;
    private OnItemClickListener mListener;

    // 定义点击事件接口
    public interface OnItemClickListener {
        void onItemClick(Product product);
    }

    // 设置点击监听器的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
        public TextView textView1,textView2;
        public ImageView imageView; // 假设你有一个ImageView在布局中
        public RecyclerViewHolder(View itemView) {
            super(itemView);
            textView1 = itemView.findViewById(R.id.tv_product_name);
            textView2 = itemView.findViewById(R.id.tv_product_introduction);
            imageView = itemView.findViewById(R.id.iv_product); // 假设你有一个ImageView在布局中
        }

    }

    public RecyclerViewAdapter2 (List<Product> data) {
        mData = data;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycler_view_shopping, parent, false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        Product item = mData.get(position);


        holder.textView1.setText(item.getName());
        holder.textView2.setText(item.getIntroduction());
        Glide.with(holder.itemView.getContext())
                .load(item.getImageUrl())
                .into(holder.imageView);
        // 设置点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onItemClick(item);
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        return mData.size();
    }
}